﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Website.Logger;
using DAL;
using BOL;
using System.Drawing;
using WebLib;
using System.Web.Script.Services;
using System.Web.Services;
using UtilityExtensions;

namespace UILGestion.ObraAdmin
{
    public partial class ComparativoAprobReadForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!this.IsPostBack)
                {

                    internalID.Value = (string)Request.QueryString["id"];
                    idObra.Value = (string)Request.QueryString["idObra"];
                    FormatForm();
                }
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
            
           
        }
        private void FormatForm()
        {
            LoadComparativo();
            
        }
        private void LoadComparativo()
        {
            ComparativoBL com = new ComparativoBL();
            ComparativoInfo info = com.LoadComparativo(int.Parse(internalID.Value));
            numComparativo.Text = info.numDocumento;
            fechaLabel.Text = info.fecha.ToShortDateString();
            estructuraCompraLabel.Text = info.estructuraCosto;
            obraLabel.Text = info.obra;
            observacionesLBL.Text = info.observaciones;
            estadoLabel.Text = info.estado.ToUpper();
            RevisadoLabel.Text = info.aprobacion1;
            DataBindCab(com);
            DataBindItem(com);

        }
        private void DataBindCab(ComparativoBL com)
        {
            var comparativo=DbUtil.Db.Comparativos.Single(p=>p.idInternal==int.Parse(internalID.Value));

            List<ComparativoProveedorInfo> List=new List<ComparativoProveedorInfo>();
            ComparativoProveedorInfo totalBase= new ComparativoProveedorInfo();
            totalBase.descripcion="TOTAL BASE E/C";
            totalBase.totalMon="{0}{1:N}".Fmt(comparativo.Moneda.simbol,comparativo.totalComparativo);
            
            List.Add(totalBase);
            List.AddRange(com.getComparativoProveedorInfo(int.Parse(internalID.Value)));

            CabView.DataSource=List;
            CabView.DataBind();
	    }


        protected void proveedorGridView_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if ((ComparativoProveedorInfo)e.Row.DataItem != null)
                {
                    if (((ComparativoProveedorInfo)e.Row.DataItem).bitSeleccion ==true)
                        
                    {
                        e.Row.BackColor = Color.FromName("#FFCCFF");

                    }


                }
            }
        }


        private void DataBindItem(ComparativoBL com)
        {

            //se carga segun la cantidad de proveedores
            int count = DbUtil.Db.ComparativoProveedors.Where(p => p.idComparativo == internalID.Value.ToInt()).Count();
            if (count == 1)
            { 
                itemComparativoGridView.Columns[9].Visible=false;
                itemComparativoGridView.Columns[10].Visible=false;
                itemComparativoGridView.Columns[11].Visible=false;
                itemComparativoGridView.Columns[12].Visible=false;
            }
            if (count == 2)
            {
                itemComparativoGridView.Columns[11].Visible = false;
                itemComparativoGridView.Columns[12].Visible = false;
               
            }
            itemComparativoGridView.DataSource=com.getComparativoItemList(int.Parse(internalID.Value));
            itemComparativoGridView.DataBind();
        }

        protected void save(object sender, EventArgs e)
        {
            try
            {
                if (((LinkButton)sender).ID == aprobar.ID)
                {
                    ComparativoBL comparativo = new ComparativoBL();

                    bool nivel1 = comparativo.updateAprobacionNivel1(int.Parse(internalID.Value));


                    bool nivel2 = comparativo.updateAprobacionNivel2(int.Parse(internalID.Value));

                    comparativo = null;
                    
                    Response.EndShowMessage("Fue Aprobado el comparativo");
                }else
                {
                    ComparativoPendienteInfo List = new ComparativoBL().getComparativoPendienteResuSiguienteList(internalID.Value.ToInt());
                    
                    if (List !=null)
                    {

                        Response.Redirect("ComparativoAprobReadForm.aspx?id={0}&idObra={1}".Fmt(List.idInternal, List.idObra));
                    }else
                    {
                        Response.EndShowMessage("No existe mas Registros pendientes por aprobacion");
                        
                    }


                }
              
                
            }

            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
        }


    }
}